package com.heima.bos.web.action.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.heima.bos.domain.Region;
import com.heima.bos.service.BaseService;
import com.heima.bos.service.RegionService;
import com.heima.bos.utils.PinYin4jUtils;
import com.heima.bos.web.action.RegionActionInter;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
@SuppressWarnings("all")
@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> implements RegionActionInter{
	@Resource
	private RegionService regionService;
	private File myFile;//属性注入
	private String myFileFileName;
	//注入文件名
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	/**
	 * 批量导入数据(解析XLs)
	 * @throws IOException 
	 */
	public String batchImport() throws IOException{
		String flag="1";
		List<Region> list = new ArrayList<Region>();
		try {
			Workbook workBook = null;
			if(myFileFileName.endsWith(".xlsx")){
				workBook = new XSSFWorkbook(new FileInputStream(myFile));
			}else{
				workBook = new HSSFWorkbook(new FileInputStream(myFile));
			}
//			读取文件
//			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(myFile));
			//获取第一张sheet
			Sheet sheet =  workBook.getSheetAt(0);
			//遍历sheet中的行
			for (Row row : sheet) {
				int rowNum = row.getRowNum();
				if(rowNum==0){
					//第一行为文件的标题行
					continue;
				}
				String id = row.getCell(0).getStringCellValue();
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();
				
				//简码 获取首字母
				String str1 = province + city + district;
				String[] sts1 = PinYin4jUtils.getHeadByString(str1);
				String shortcode = StringUtils.join(sts1, "");
				//获取城市简码
				String[] sts2 = PinYin4jUtils.stringToPinyin(city);
				String citycode = StringUtils.join(sts2);
				
				Region region = new Region(id, province, city, district, postcode);
				region.setShortcode(shortcode);
				region.setCitycode(citycode);
				list.add(region);
			}
			regionService.batchImport(list);
		} catch (IOException e) {
			e.printStackTrace();
			flag="0";
		}
		ServletActionContext.getResponse().getWriter().print(flag);
		return NONE;
	}
	/**
	 * 分页查询
	 */
	public String pageQuery(){
		pageList();
		String[] excludes = new String[]{"page","pageSize","detachedCriteria","subareas"};
		writePageBean2Json(pageBean, excludes);
		return NONE;
	}
	/**
	 * subArea下拉列表显示region数据
	 */
	public String findAllJson(){
		
		List<Region> list = regionService.findAll();
		String[] excludes = new String[]{"subareas"};
		writeList2Json(list, excludes);
		return NONE;
	}
	@Override
	protected BaseService<Region> getService() {
		return regionService;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
}
